package com.fh.controller;

import com.fh.common.ResultData;
import com.fh.entity.po.User;
import com.fh.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @RequestMapping("userLogin")
    @ResponseBody
    public ResultData userLogin(String userName,String password){
        if (StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)){
            throw new RuntimeException("用户名或密码不能为空");
        }
        return ResultData.success(loginService.userLogin(userName,password));
    }

    @RequestMapping("loginOut")
    public String loginOut(HttpServletRequest request){
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        if (userInfo!=null){
            request.getSession().invalidate();
        }
        return "redirect:/login.jsp";
    }
}
